<script setup lang="ts">
import { getCodeAPI, postLoginAPI } from '@/apis/common';
import { useUserStore } from '@/stores/user';
import type { PostLoginParams } from '@/types/common';
import { ref } from 'vue';

  defineOptions({
    options: {
      styleIsolation: 'shared',
    },
  })
  // 1. 倒计时处理
const showCountDown = ref(false)
const getMobileCode = async () => {
  await formRef.value?.validateField?.(['mobile'])
  showCountDown.value = true
  // 调用接口，通知后端发送验证码到用户手机
  const res = await getCodeAPI({ mobile: formData.value.mobile, type: 'login' })
  console.log('测试阶段返回值有验证码', res)
}
const Fn = () => {
  // console.log('倒计时结束');
  // 场景：倒计时结束，用户没有收到验证码，可以重新获取验证码
  showCountDown.value = false
}
// 1.准备表单数据
const formData = ref<PostLoginParams>({
  mobile: '13230000027',
  code: '',
})
// 2.表单校验规则
const formRules = ref<UniHelper.UniFormsRules>({
  mobile: {
    rules: [
      { required: true, errorMessage: '请输入手机号' },
      { pattern: /^1[3-9]\d{9}$/, errorMessage: '请输入正确的手机号' },
    ],
  },
  code: {
    rules: [
      { required: true, errorMessage: '请输入验证码' },
      { pattern: /^\d{6}$/, errorMessage: '验证码格式错误' },
    ],
  },
})
// 3. 点击登录校验整个表单
const formRef = ref<UniHelper.UniFormsInstance>()
const isAgree = ref(false)
const handleLogin = async () => {
  if (!isAgree.value) {
    return uni.showToast({ title: '请同意用户协议和隐私协议', icon: 'none' })
  }
  await formRef.value?.validate?.()
  // 发送表单给后端，获取登录结果
  const res = await postLoginAPI(formData.value)
  console.log('登录结果', res)
  const store = useUserStore()
  store.setToken(res.data.token)

  store.setUserInfo(res.data)
  uni.showToast({ title: '登录成功', icon: 'success' })
  uni.switchTab({ url: '/pages/index/index' })
}

</script>

<template>
  <uni-forms class="login-form"
   ref="formRef" 
  :model="formData" 
  :rules="formRules" >
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
        v-model="formData.mobile"
        :maxlength="11"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
        v-model="formData.code"
        :maxlength="6"
      />
      <text class="text-button" v-if="!showCountDown" @click="getMobileCode" >获取验证码</text>
      <view class="text-button" v-else>
        <uni-countdown 
        :show-day="false" 
        :show-hour="false" 
        :show-minute="false" 
        :second="59"
        @timeup="Fn"
        />秒后重新发送
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="isAgree" @click="isAgree = !isAgree" color="#16C2A3" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @tap="handleLogin">登 录</button>
  </uni-forms>
  {{ formData }}
</template>

<style lang="scss">
  @import './styles.scss';
</style>
